package org.dolphin.action.district;

import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.dolphin.base.bean.CityVO;
import org.dolphin.base.bean.RegionVO;
import org.dolphin.base.json.PaginationBean;
import org.dolphin.service.DistrictService;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "custom-default")
@Namespace(value = "/district")
@InterceptorRefs(value = {@InterceptorRef(value = "customStack")})
public class DistrictAction extends ActionSupport {
  private static final long serialVersionUID = 2474231159915839026L;
  private DistrictService districtService;
  private PaginationBean pagination;
  private Long region;

  public DistrictService getDistrictService() {
    return districtService;
  }

  public PaginationBean getPagination() {
    return pagination;
  }

  public Long getRegion() {
    return region;
  }

  @Action(value = "getCities", results = @Result(name = "success", params = {"root", "pagination"}, type = "json"))
  public String listCities() {
    List<Long> regions = new ArrayList<Long>(1);
    if (getRegion() != null) {
      regions.add(getRegion());
    }
    List<CityVO> cities = getDistrictService().getCities(regions);
    pagination = new PaginationBean();
    pagination.setRows(cities);
    return SUCCESS;
  }

  @Action(value = "getRegions", results = @Result(name = "success", params = {"root", "pagination"}, type = "json"))
  public String listRegions() {
    List<RegionVO> regions = getDistrictService().getRegions();
    pagination = new PaginationBean();
    pagination.setRows(regions);
    return SUCCESS;
  }

  public void setDistrictService(DistrictService districtService) {
    this.districtService = districtService;
  }

  public void setPagination(PaginationBean pagination) {
    this.pagination = pagination;
  }

  public void setRegion(Long region) {
    this.region = region;
  }
}
